using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WebFoundation.Data;
using DDHDeepTunnel.DeepScheduling.Interfaces;

namespace DDHDeepTunnel.DeepScheduling.Data
{
    public class SystemLogQueryDAO : QueriableDAO<SystemLog>
    {
        public static DbManager _dbManager = DbManager.FromConfig("DefaultConnection");
        public SystemLogQueryDAO()
            : base(_dbManager)
        {

        }

        public SystemLogQueryDAO(DbManager dbManager)
            : base(dbManager)
        {
            _dbManager = dbManager;
        }

        public IEnumerable<SystemLog> GetAllSystemLogInfo()
        {
            var query = this.CreateQuery();
            query.OrderBy(r => r.LogTime, false);
            return this.Query(query);
        }

        public IEnumerable<SystemLog> GetSystemLogInfoByLogType(string logType)
        {
            var query = this.CreateQuery();
            query.Filter().Eq(r => r.LogType, logType).And().Eq(r => r.StateInfo, 1).And().Eq(r => r.Isuse, 1);
            query.OrderBy(r => r.LogTime, false);
            return this.Query(query);
        }

        public IEnumerable<SystemLog> GetSystemLogInfoByLogType(string logType, DateTime startTime,
            DateTime endTime)
        {
            var query = this.CreateQuery();
            query.Filter().Eq(r => r.LogType, logType).And().Between(r => r.LogTime, startTime, endTime).And().Eq(r => r.StateInfo, 1).And().Eq(r => r.Isuse, 1);
            query.OrderBy(r => r.LogTime, false);
            return this.Query(query);
        }

        public IEnumerable<SystemLog> GetSystemLogInfoByLogTypes(string logTypes, DateTime startTime,
            DateTime endTime)
        {
            var lTypes = logTypes.Split(',');
            var query = this.CreateQuery();
            var filter = query.Filter().Eq(r => r.LogType, lTypes[0]).Or();
            for (var i = 1; i < lTypes.Length; i++)
            {
                filter.Eq(r => r.LogType, lTypes[i]);
            }
            filter.And().Between(r => r.LogTime, startTime, endTime).And().Eq(r => r.StateInfo, 1).And().Eq(r => r.Isuse, 1);
            query.OrderBy(r => r.LogTime, false);
            return this.Query(query);
        }
    }
}